<template>
	<view class="content">
		<view class="user_info_row">
			<view class="user_info">
				<view>{{address.userName}}</view>
				<view>{{address.all}}</view>
			</view>
			<view class="user_phone">{{address.telNumber}}</view>
		</view>
		<view class="btn" @click="reChooseAddress">
			修改收货地址
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				address: {}
			}
		},
		onShow() {
			this.handleChooseAddress()
		},
		methods: {
			async handleChooseAddress() {
				try {
					// 1 获取 权限状态
					const res1 = await uni.getSetting();
					const scopeAddress = res1[1].authSetting["scope.address"];
					// 2 判断 权限状态
					if (scopeAddress === false) {
						await uni.openSetting();
					}
					// 4 调用获取收货地址的 api
					let addressStorage = uni.getStorageSync('address')
					if (addressStorage == '') {
						let addresss = await uni.chooseAddress();
						// console.log(addresss[1])
						let address = addresss[1]
						address.all = address.provinceName + address.cityName + address.countyName + address
							.detailInfo;
						// 5 存入到缓存中
						uni.setStorageSync("address", address);
						this.address = address.all
						// console.log(this.address)
					} else {
						addressStorage.all = addressStorage.provinceName + addressStorage.cityName + addressStorage
							.countyName + addressStorage.detailInfo;
						this.address = addressStorage
					}
					uni.redirectTo({
						url: 'pages/adress/adress'
					})

				} catch (error) {
					// console.log(error);
				}
			},
			async reChooseAddress() {
				let addresss = await uni.chooseAddress();
				// console.log(addresss)
				let address = addresss[1]
				address.all = address.provinceName + address.cityName + address.countyName + address
					.detailInfo;
				// 5 存入到缓存中
				uni.setStorageSync("address", address);
				this.address = address.all
			}
		}
	}
</script>

<style scoped>
	.user_info_row {
		display: flex;
		padding: 20rpx;
		background-color: #b3dedd;
		border-radius: 30upx;
		box-shadow: 0 0 10upx #888;
		margin: 20upx;
	}

	.user_info_row .user_info {
		flex: 5;
	}

	.user_info_row .user_phone {
		flex: 3;
		text-align: right;
	}

	.btn {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 36upx;
		color: white;
		width: 250upx;
		height: 80upx;
		margin-top: 100upx;
		margin-left: 65%;
		background-color: tomato;
		border-radius: 20upx;
	}
</style>
